#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File     : SubLayers.py
# @Time     : 2024/8/14 15:43


import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np

from Modules import ScaledDotProductAttention


class MultiHeadAttention(nn.Module):
    """多头注意力"""

    def __init__(self, n_head, d_model, d_k, d_v, dropout=0.1):
        """
        n_head: 注意力头的数量 8
        d_model: 模型的维度 512
        d_k: 每个注意力头的键（key）的维度 64
        d_v: 每个注意力头的值（value）的维度
        dropout: dropout 率，默认为 0.1
        """
        super.__init__()
        self.n_head = n_head
        self.d_k = d_k
        self.d_v = d_v

        self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False)
        self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False)
        self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False)
        self.fc = nn.Linear(n_head * d_v, d_model, bias=False)

        self.attention = ScaledDotProductAttention(temperature=d_k ** 0.5)

        self.dropout = nn.Dropout(dropout)
        self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)

    def forward(self, q, k, v, mask=None):
        d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
        # sz_b：批次大小（batch-size）
        sz_b, len_q, len_k, len_v = q.size(0), q.size(1), k.size(1), v.size(1)

        residual = q
        # b x lq x d_model
        print(f'q.shape:{residual.size}')

        # 1、b x lq x d_model --> b x lq x (n*dv)
        # Separate different heads: b x lq x n x dv
        q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
        k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
        v = self.w_qs(v).view(sz_b, len_v, n_head, d_v)

        # Transpose for attention dot product: b x n x lq x dv
        q, k, v = q.transpose(1, 2), k.transpose(1, 2), v.transpose(1, 2)

        if mask is not None:
            # 如果存在 mask，则将其形状扩展，以便与多头的维度兼容
            mask = mask.unsqueeze(1)

        # 输出 q 是注意力的结果，而 attn 是注意力权重矩阵。
        # attn: softmax(Q * K^T / sqrt(d_K))
        # q: softmax(Q * K^T / sqrt(d_K)) * V
        # Decoder只有Q来自于上一个Decoder单元的输出，K与V都来自于Encoder最后一层的输出
        q, attn = self.attention(q, k, v, mask=mask)

        # 重组并连接所有头的输出
        # b x n x lq x dv --> b x lq x n x dv --> b x lq x (n*dv)
        q = q.transpose(1, 2).contiguous.view(sz_b, len_q, -1)

        # fc: b x lq x (n*dv) --> b x lq x d_model
        q = self.dropout(self.fc(q))

        # add & norm
        q += residual
        q = self.layer_norm(q)

        # q: b x lq x d_model
        # attn: b x n x lq x lq
        return q, attn


class PositionwiseFeedForward(nn.Module):
    """前向传播FFN ADD NORM"""

    def __init__(self, d_in, d_hid, dropout=0.1):
        super().__init__()
        self.w_1 = nn.Linear(d_in, d_hid)
        self.w_2 = nn.Linear(d_hid, d_in)
        self.layer_norm = nn.LayerNorm(d_in, 1e-6)
        self.dropout = nn.Dropout(dropout)

    def forward(self, x):
        residual = x
        x = self.w_2(F.relu(self.w_1(x)))
        x = self.dropout(x)

        # add & norm
        x += residual
        x = self.layer_norm(x)
        return x